<?php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use League\Flysystem\Config;
use Illuminate\Support\Facades\Validator;


class IndexController extends Controller
{

    protected $user;
    public function __construct(UserRepository $user)
    {
        $this->user=$user;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function login(Request $request)
    {
        if(Input::all()){
            Validator::make($request->input(),[
                'userName'=>'required',
                'password'=>'required'
            ],[
                'userName.required'=>'用户名不能为空',
                'password.required'=>'密码不能为空'
            ]);

            $result=$this->user->handleLogin(Input::all());

            if($result['status_code']!=200){

                return back()->with('errors',$result['message']);
            }else{
                $request->session()->put(\config('constants.SESSION_NAME'),$request->input('userName'));
                return redirect('admin/index');
            }
        }else{
            return view('admin.login');
        }

    }

    /**
     * 退出登录接口
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout(Request $request)
    {
        if($request->session()->has(\config('constants.SESSION_NAME'))){
            $request->session()->forget(\config('constants.SESSION_NAME'));
        }
        return redirect('login');

    }
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.index');

    }
}
